package com.eastedu.report.common.util;

import java.text.DecimalFormat;

/**
 * 浮点数处理
 *
 * @author wanqt
 */
public interface DecimalUtil {

    /**
     * 保留两位小数
     *
     * @param num    the num
     * @param format the format
     * @return the double
     * @author wanqt
     */
    static double doubleFormat(double num, String format) {
        DecimalFormat df = new DecimalFormat(format);
        return Double.parseDouble(df.format(num));
    }

    /**
     * 保留两位小数
     *
     * @param num the num
     * @return the double
     * @author wanqt
     */
    static double doubleFormat(double num) {
        return doubleFormat(num, "#0.00");
    }

    /**
     * 保留四位小数
     *
     * @param num the num
     * @return the double
     * @author wanqt
     */
    static double keepFourDecimalPlaces(double num) {
        return doubleFormat(num, "#0.0000");
    }

    /**
     * 处理除法，分子分母
     *
     * @param numerator   the numerator
     * @param denominator the denominator
     * @return the double
     * @author wanqt
     */
    static double rateCalculation(Integer numerator, Integer denominator) {
        if (null == numerator || 0 == numerator || null == denominator || 0 == denominator) {
            return 0d;
        }
        return (double) numerator / denominator;
    }
}
